package com.amor.net

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.amor.util.PathUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.transition.Transition
import com.hy.frame.common.IImageLoader

/**
 * title 图片加载器
 * author HeYan
 * date 2021/9/25
 * desc
 */
class ImageLoader : IImageLoader {

    private var glide: RequestManager? = null
    private var cxt: Context? = null
    private var act: Activity? = null
    private var fragment: Fragment? = null

    constructor(cxt: Context) {
        this.cxt = cxt
    }

    constructor(cxt: Activity) {
        this.act = cxt
    }

    constructor(cxt: Fragment) {
        this.fragment = cxt
    }

    private fun getGlide(): RequestManager? {
        if (glide == null) {
            glide = when {
                fragment != null -> Glide.with(fragment!!)
                act != null -> Glide.with(act!!)
                cxt != null -> Glide.with(cxt!!)
                else -> null
            }
        }
        return glide
    }

    override fun load(v: View?, url: String?) {
        if (v == null) return
        if (url.isNullOrEmpty()) return
        load(v, IImageLoader.ImageInfo.build(url, 0, 0, false))
    }

    override fun load(v: View?, info: IImageLoader.ImageInfo) {
        if (v == null) return
        info.url = PathUtil.checkFileURL(info.url)
        //info.isGif = info.url != null && info.url.endsWith(".gif")
        if (info.listener != null)
            info.listener.onLoading(v)
        else if (info.loading != 0)
            setImage(v, info.loading)
        if (info.url.isNullOrBlank()) {
            if (info.listener != null)
                info.listener.onLoadComplete(v, null)
            else if (info.fail != 0)
                setImage(v, info.fail)
            return
        }
        try {
            getGlide()?.asDrawable()?.load(info.url)?.into(object : CustomViewTarget<View, Drawable>(v) {

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    loadFail(v, info)
                }

                override fun onResourceCleared(placeholder: Drawable?) {
                }

                override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                    if (info.listener != null)
                        info.listener.onLoadComplete(v, resource)
                    else
                        setImage(v, resource)
                }

            })
        } catch (e: Exception) {
            e.printStackTrace()
            loadFail(v, info)
        }
    }

    private fun loadFail(v: View, info: IImageLoader.ImageInfo) {
        if (info.listener != null)
            info.listener.onLoadComplete(v, null)
        else if (info.fail != 0)
            setImage(v, info.fail)
    }

    private fun setImage(v: View, res: Int) {
        if (v is ImageView)
            v.setImageResource(res)
        else
            v.setBackgroundResource(res)
    }


    private fun setImage(v: View, res: Drawable) {
        if (v is ImageView)
            v.setImageDrawable(res)
        else
            v.background = res
        if (res is GifDrawable)
            res.start()
    }

    override fun onPause() {
    }

    override fun onResume() {

    }

    override fun onDestroy() {
    }
}